package com.dianrui.firemenmonitor.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dianrui.firemenmonitor.R;
import com.dianrui.firemenmonitor.bean.FieldBean;
import com.dianrui.firemenmonitor.util.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FieldListAdapters extends BaseAdapter {

    private List<FieldBean> attendaceBeanList;
    private Context context;

    public FieldListAdapters(Context context, List<FieldBean> mainArticlesList) {
        this.context = context;
        this.attendaceBeanList = mainArticlesList;

    }

    public void refresh(List<FieldBean> list) {
        this.attendaceBeanList = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (attendaceBeanList.size() == 0) {
            return 0;
        }
        return attendaceBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return attendaceBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fields, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        //status1是外勤，2是完勤
        if (attendaceBeanList != null && attendaceBeanList.size() > 0) {
            FieldBean fieldBean = attendaceBeanList.get(position);
            if (fieldBean.status == 1) {
                holder.all_layout.setVisibility(View.VISIBLE);
                holder.content.setText(parent.getContext().getResources().getString(R.string.fieldwork_txt) + "  " + fieldBean.inputtime);
                if (StringUtils.isEmpty(fieldBean.address)) {
                    holder.address.setText("暂无位置信息");
                } else {
                    holder.address.setText(fieldBean.address);
                }
            } else if (fieldBean.status == 2) {
                holder.all_layout.setVisibility(View.VISIBLE);
                holder.content.setText(parent.getContext().getResources().getString(R.string.fieldworks_txt) + "  " + fieldBean.inputtime);
                if (StringUtils.isEmpty(fieldBean.address)) {
                    holder.address.setText("暂无位置信息");
                } else {
                    holder.address.setText(fieldBean.address);
                }
            }

        }

        return convertView;
    }


    class ViewHolder {

        @BindView(R.id.tvDot)
        TextView tvDot;
        @BindView(R.id.rlTimeline)
        RelativeLayout rlTimeline;
        @BindView(R.id.content)
        TextView content;
        @BindView(R.id.address_layout)
        LinearLayout addressLayout;
        @BindView(R.id.item_attendce_img)
        ImageView itemAttendceImg;
        @BindView(R.id.address)
        TextView address;
        @BindView(R.id.rlCenter)
        LinearLayout rlCenter;
        @BindView(R.id.all_layout)
        LinearLayout all_layout;



        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
